package com.bw.user_module.user;

import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.network_sdk.BaseEntity;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserViewModel extends BaseViewModel<UserRespository> {

    public ObservableField<String>nickName = new ObservableField<>("登录/注册");
    public ObservableField<String>headPath = new ObservableField<>("");
    private CheckLoginApi checkLoginApi;
    public void edtShipe(){
        if (!checkLoginApi.checkLogin()){
            startPage("/user/loginactivity");
            return;
        }
    }

    public void userOrder(){
        if (!checkLoginApi.checkLogin()){
            startPage("/user/loginactivity");
            return;
        }
    }

    public void completeOrder(){
        if (!checkLoginApi.checkLogin()){
            startPage("/user/loginactivity");
            return;
        }
    }

    public void payOrder(){
        if (!checkLoginApi.checkLogin()){
            startPage("/user/loginactivity");
            return;
        }
    }

    public void receivingOrder(){
        if (!checkLoginApi.checkLogin()){
            startPage("/user/loginactivity");
            return;
        }
    }

    public void login(){
        if (checkLoginApi.checkLogin()){
            //跳转到编辑界面
        }else
            startPage("/user/loginactivity");
    }

    public void edtUser(){
        if (checkLoginApi.checkLogin()){
            //跳转到编辑界面
            startPage("/user/edtuser");
        }else
            startPage("/user/loginactivity");
    }

    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) entity;
            if (userEntity.message.equals("用户未登录")){
                nickName.set("登录/注册");
            }else {
                nickName.set(userEntity.data.user_nick_name);
                headPath.set(userEntity.data.user_icon);
            }
        }
    }

    @Override
    protected void initData() {
        checkLoginApi = (CheckLoginApi) ARouter.getInstance()
                .build("/user/checklogin").navigation();
        if (checkLoginApi.checkLogin()){
            //获取用户详细信息
            respository.queryUser().subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }
    //刷新用户信息方法
    public void refreshUser(){
        respository.queryUser().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

}
